<#if isWithPackage?exists && isWithPackage==true>package ${packageName}.controller;</#if>
<#if isAutoImport?exists && isAutoImport==true>
import ${packageName}.entity.${classInfo.className};
import ${packageName}.repository.${classInfo.className}Repository;
import org.springframework.transaction.annotation.Transactional;
import com.metareco.common.utils.StringUtils;
import com.metareco.fund.service.pojo.common.GlobalValue;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.core.types.dsl.Expressions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

</#if>
@Service
public class ${classInfo.className}Service {
    @Autowired
    private GlobalValue globalValue;

    @Autowired
    private ${classInfo.className}Repository ${classInfo.className?uncap_first}Repository;

    public ${classInfo.className}DTO convert${classInfo.className}(${classInfo.className} ${classInfo.className?uncap_first}) {
        if (${classInfo.className?uncap_first} == null) {
            return null;
        }
        ${classInfo.className}DTO ${classInfo.className?uncap_first}DTO = new ${classInfo.className}DTO();
<#if classInfo.fieldList?exists && classInfo.fieldList?size gt 0>
    <#list classInfo.fieldList as fieldItem >
        ${classInfo.className?uncap_first}DTO.set${fieldItem.fieldName?cap_first}(${classInfo.className?uncap_first}.get${fieldItem.fieldName?cap_first}());
    </#list>
</#if>
        return ${classInfo.className?uncap_first}DTO;
    }

    public Page<${classInfo.className}DTO> list(Integer id, String keyword,
        int page, int size, String sortField, Sort.Direction sortDirection) {
        keyword = StringUtils.trimToEmpty(keyword);
        Q${classInfo.className} q${classInfo.className} = Q${classInfo.className}.${classInfo.className?uncap_first};
        final PageRequest pageRequest = PageRequest.of(page, size, sortDirection, sortField);
        BooleanBuilder finalExpr = new BooleanBuilder();
        if (id != null) {
            finalExpr = finalExpr.and(q${classInfo.className}.id.eq(id));
        }
        if (StringUtils.isNotBlank(keyword)) {
            keyword = "%" + keyword + "%";
            BooleanBuilder keywordExpr = new BooleanBuilder();
    <#list classInfo.fieldList as fieldItem >
        <#if fieldItem.fieldClass == "String">
            BooleanExpression ${fieldItem.fieldName}Expr = q${classInfo.className}.${fieldItem.fieldName}.like(keyword);
            keywordExpr = keywordExpr.or(${fieldItem.fieldName}Expr);
        </#if>
    </#list>
            finalExpr = finalExpr.and(keywordExpr);
        }

        Page<${classInfo.className}> pageRawData = ${classInfo.className?uncap_first}Repository.findAll(finalExpr, pageRequest);
        Page<${classInfo.className}DTO> pageData = pageRawData.map(this::convert${classInfo.className});
        return pageData;
    }

    public Page<${classInfo.className}DTO> export(Integer id, String keyword,
        int page, int size, String sortField, Sort.Direction sortDirection) {
        Page<${classInfo.className}DTO> pageData = this.list(id, keyword, page, size, sortField, sortDirection);
        List<${classInfo.className}DTO> dataList = pageData.getContent();

        File tmpFolder = new File(globalValue.getLocalTempFolder());
        try {
            String excelFileName = tmpFolder.getAbsolutePath() + File.separator + "ExcelFile" + DateUtils.nowAsStr() + ".xlsx";
            ExcelWriter excelWriter = new ExcelWriter(excelFileName);
            int[] freezePaneParam = {7, 1, 7, 1};
            excelWriter.writeSheet(dataList, ChinaFundMajorEventDTO.class, freezePaneParam);
            byte[] content = IOUtils.toByteArray(new FileInputStream(excelFileName));
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("fileName", "TODO.xlsx");
            resultMap.put("fileContent", content);
            return resultMap;
        } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw new FMException("导出失败");
        }
    }

    @Transactional
    public void update(${classInfo.className}DTO param) {
        boolean isInsert = param.getId() == null;
        ${classInfo.className} ${classInfo.className?uncap_first} = isInsert ? new ${classInfo.className}() : ${classInfo.className?uncap_first}Repository.getById(param.getId());
<#if classInfo.fieldList?exists && classInfo.fieldList?size gt 0>
    <#list classInfo.fieldList as fieldItem >
        ${classInfo.className?uncap_first}.set${fieldItem.fieldName?cap_first}(param.get${fieldItem.fieldName?cap_first}());
    </#list>
</#if>
        ${classInfo.className?uncap_first}Repository.save(${classInfo.className?uncap_first});
    }

    @Transactional
    public void delete(int id) {
        ${classInfo.className?uncap_first}Repository.deleteById(id);
    }

}
